package com.example.hehecat.yuejian1.ui.fragment;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hehecat.yuejian1.R;
import com.example.hehecat.yuejian1.mvp.contract.GameInfoContract;
import com.example.hehecat.yuejian1.mvp.model.GameInfoModel;
import com.example.hehecat.yuejian1.mvp.model.api.Api;
import com.example.hehecat.yuejian1.mvp.model.bean.GameBean;
import com.example.hehecat.yuejian1.mvp.presenter.GameInfoPresenter;
import com.example.hehecat.yuejian1.ui.wiget.MyStaticVariable;
import com.ldoublem.thumbUplib.ThumbUpView;
import com.squareup.picasso.Picasso;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;
import rx.Subscriber;

import static com.chad.library.adapter.base.listener.SimpleClickListener.TAG;

public class GameInfoFragment extends SupportFragment implements GameInfoContract.View {


    GameInfoPresenter presenter;
    int game_id;
    int user_id;

    Unbinder unbinder;
    @BindView(R.id.game_share)
    Button gameShare;
    @BindView(R.id.game_tpv)
    ThumbUpView gameTpv;
    @BindView(R.id.game_img)
    ImageView gameImg;
    @BindView(R.id.title_game)
    AppCompatTextView titleGame;
    @BindView(R.id.content_game)
    AppCompatTextView contentGame;
    @BindView(R.id.game_start)
    TextView gameStart;
    @BindView(R.id.game_end)
    TextView gameEnd;

    @OnClick(R.id.gameinfo_return)
    void gameReturn() {
        MainFragment fragment = new MainFragment();
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content, fragment, null)
                .commit();
    }


    @OnClick(R.id.game_share)
    void btShareOnClick() {
        Intent textIntent = new Intent(Intent.ACTION_SEND);
        textIntent.setType("text/plain");
        textIntent.putExtra(Intent.EXTRA_TEXT, contentGame.getText());
        startActivity(Intent.createChooser(textIntent, "分享"));
    }



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_game_info, container, false);
        presenter = new GameInfoPresenter(this);
        unbinder = ButterKnife.bind(this, view);


        if (getArguments() != null) {
            game_id = getArguments().getInt("game_id");
        }
        user_id = MyStaticVariable.newInstance().getUserid();
        Log.d(TAG, "onCreateView:game_id " + game_id);


        //点赞
        isGameCollect();
        gameTpv.setUnLikeType(ThumbUpView.LikeType.broken);
        gameTpv.setCracksColor(Color.rgb(22, 33, 44));
        gameTpv.setFillColor(Color.rgb(11, 200, 77));
        gameTpv.setEdgeColor(Color.rgb(33, 3, 219));
        gameTpv.setOnThumbUp(like -> collectGame());


        //获取详情
        GameInfoModel model = new GameInfoModel();
        model.getGame(game_id, new Subscriber<GameBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(GameBean gameBean) {
                Log.d(TAG, "onNext: " + gameBean.getGamename());
                gameStart.setText(gameBean.getStartdate() + "至");
                gameEnd.setText(gameBean.getEnddate());
                contentGame.setText(gameBean.getIntro());
                Picasso.with(getContext()).load(Api.IMG_URL + gameBean.getGameimg()).into(gameImg);
            }
        });
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void showMsg(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setCollect() {
        gameTpv.setLike();
    }

    @Override
    public void setUnCollect() {
        gameTpv.setUnlike();
    }

    @Override
    public void collectGame() {
        presenter.CollectGame(user_id, game_id);
    }

    @Override
    public void isGameCollect() {
        presenter.isCollected(user_id, game_id);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();

    }

    public static GameInfoFragment newInstance(int game_id) {
        GameInfoFragment fragment = new GameInfoFragment();
        Bundle args = new Bundle();
        args.putInt("game_id", game_id);
        fragment.setArguments(args);
        return fragment;
    }
}
